//
// Created by zhengjuping on 24-9-12.
//

#ifndef ENV_II_BT_ENVIIGATTCHARACTERISTIC_H
#define ENV_II_BT_ENVIIGATTCHARACTERISTIC_H
#include "GattCharacteristic.h"
#include "SensorManager.h"
#include "EnvIIGattCharacteristicDesc.h"

class EnvIIGattCharacteristic : public GattCharacteristic,
                                public ISensorObserver,
                                public ICharacteristicNotifyObserver {
public:
    EnvIIGattCharacteristic();
    virtual ~EnvIIGattCharacteristic();


private:
    void onSensorValueUpdata(float temperature, float humidity, float pressure) override;
    void onReadEvent( uint8_t *data, uint16_t &length ) override;
    void onWriteEvent(const uint8_t *data, uint16_t length ) override;
    void onNotifyStatusUpdateEvent( bool notifyEnabled ) override;
    void onNotify(bool enable) override;

    void convertToEnvValue(float temperature, float humidity, float pressure, uint8_t *envValue) const;

    std::unique_ptr<SensorManager> _sensorManager = nullptr;
    std::mutex _mutex;
    std::atomic<bool> _isNotifyEnabled = false;


    float _temperature = 0;
    float _humidity = 0;
    float _pressure = 0;

    //Eg
    //0x98, 0x0A 温度 -> 0x0a98 2712/100 = 27.12
    //0x00, 0x14 湿度 -> 0x1400 5120/100 = 51.2
    //0x1f, 0x9c, 0x9a, 0x00 压力 0x009a9c1f = 10132511/100 = 101325.11
    uint8_t _envValue[8]; //大端字节序
};


#endif //ENV_II_BT_ENVIIGATTCHARACTERISTIC_H
